package br.com.mateusfsilva.truco;

import java.util.HashMap;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.RepeatingSpriteBackground;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.source.AssetTextureSource;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.view.Display;
import br.com.mateusfsilva.truco.card.Card;

public class Truco extends BaseGameActivity
{
   // ===========================================================
   // Constants
   // ===========================================================

   private int CAMERA_WIDTH = 800;
   private int CAMERA_HEIGHT = 480;
   private double RATIO_CAMERA_WIDTH = 1.0f;
   private double RATIO_CAMERA_HEIGHT = 1.0f;

   // ===========================================================
   // Fields
   // ===========================================================

   private Camera mCamera;
   private Texture mCardDeckTexture;
   private RepeatingSpriteBackground mBackground;
   
   private Match match;

   private HashMap<Card, TextureRegion> mCardTotextureRegionMap;

   // ===========================================================
   // Constructors
   // ===========================================================

   // ===========================================================
   // Getter & Setter
   // ===========================================================
   
   public double getRATIO_CAMERA_WIDTH()
   {
      return RATIO_CAMERA_WIDTH;
   }
   
   public double getRATIO_CAMERA_HEIGHT()
   {
      return RATIO_CAMERA_HEIGHT;
   }

   public HashMap<Card, TextureRegion> getmCardTotextureRegionMap()
   {
      return mCardTotextureRegionMap;
   }
   
   // ===========================================================
   // Methods for/from SuperClass/Interfaces
   // ===========================================================

   @Override
   public Engine onLoadEngine()
   {
      Display d = getWindowManager().getDefaultDisplay();
      this.RATIO_CAMERA_WIDTH = (double)d.getWidth() / this.CAMERA_WIDTH;
      this.RATIO_CAMERA_HEIGHT = (double)d.getHeight() / this.CAMERA_HEIGHT;
      this.CAMERA_WIDTH = d.getWidth();
      this.CAMERA_HEIGHT = d.getHeight();
      
      this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
      final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));

      return engine;
   }

   @Override
   public void onLoadResources()
   {
      this.mCardDeckTexture = new Texture(1024, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

      TextureRegionFactory.createFromAsset(this.mCardDeckTexture, this, "gfx/carddeck_tiled.png", 0, 0);

      this.mCardTotextureRegionMap = new HashMap<Card, TextureRegion>();
      
      /* Extract the TextureRegion of each card in the whole deck. */
      for(final Card card : Card.values())
      {
         final TextureRegion cardTextureRegion = TextureRegionFactory.extractFromTexture(this.mCardDeckTexture, card.getTexturePositionX(), card.getTexturePositionY(), Card.CARD_WIDTH, Card.CARD_HEIGHT);
         this.mCardTotextureRegionMap.put(card, cardTextureRegion);
      }

      this.mEngine.getTextureManager().loadTexture(this.mCardDeckTexture);
      
      this.mBackground = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, this.mEngine.getTextureManager(), new AssetTextureSource(this, "gfx/green-suited-poker-table-felt-75.png"));
   }

   @Override
   public Scene onLoadScene()
   {
      this.mEngine.registerUpdateHandler(new FPSLogger());

      final Scene scene = new Scene(1);
      scene.setOnAreaTouchTraversalFrontToBack();

      this.match = new Match(this, scene);
      
      scene.setBackground(this.mBackground);

      scene.setTouchAreaBindingEnabled(true);

      return scene;
   }

   @Override
   public void onLoadComplete()
   {

   }

   // ===========================================================
   // Methods
   // ===========================================================

   // ===========================================================
   // Inner and Anonymous Classes
   // ===========================================================
}